﻿using System;
using System.Collections.Generic;
using System.Text;
using Bule.Malbilden.Domain;
using Bule.Malbilden.Configuration;

namespace Bule.Malbilden.Domain
{
    public class NutritionStatisticsCalculator
    {
        private NutritionData recommendedNutrition;
        private NutritionData totalNutrition;
        private DayNutritionGoal goal;

        public NutritionStatisticsCalculator(Person person, DateTime date)
        {
            IRdaConfigurationReader reader = new XmlRdaConfigurationReader("Xml/Rda.xml");
            recommendedNutrition = reader.GetRecommendation(person);

            totalNutrition = person.GetTotalEatenNutritionalValue(date);
            goal = person.GetNutritionGoal(date);
            if (goal == null)
                goal = new DayNutritionGoal();
        }

        public NutritionStatisticsCalculator(NutritionData recommendedDietaryAllowance)
        {
            this.recommendedNutrition = recommendedDietaryAllowance;
        }

        public double GetReachedPercentage(NutritionData.MacroNutrient nutrient, double amount)
        {
            double rdaValue = recommendedNutrition.GetMacroNutrient(nutrient);

            if (rdaValue == 0)
                return 0;
            else
                return 100 * amount / rdaValue;
        }

        public double GetReachedPercentage(NutritionData.MicroNutrient nutrient, double amount)
        {
            double rdaValue = recommendedNutrition.GetMicroNutrient(nutrient);

            if (rdaValue == 0)
                return 0;
            else
                return 100 * amount / rdaValue;
        }

        public double GetReachedPercentage(double amount, double total)
        {
            if (total <= 0)
                return 0;

            return amount / total * 100;
        }

        public double GetEnergyPercentage(NutritionData.MacroNutrient nutrient)
        {
            return totalNutrition.GetEnergyPercentage(nutrient);
        }

        public double GetProteinGoalPercentage()
        {
            double proteinEnergyPercent = GetReachedPercentage(GetTotalProteinEnergyInKcal(), goal.Kcal);

            return GetReachedPercentage(proteinEnergyPercent, goal.ProteinEnergyPercentage);
        }

        public double GetCarbGoalPercentage()
        {
            double carbEnergyPercent = GetReachedPercentage(GetTotalCarbohydrateEnergyInKcal(), goal.Kcal);

            return GetReachedPercentage(carbEnergyPercent, goal.CarbohydrateEnergyPercentage);
        }

        public double GetFatGoalPercentage()
        {
            double fatEnergyPercent = GetReachedPercentage(GetTotalFatEnergyInKcal(), goal.Kcal);

            return GetReachedPercentage(fatEnergyPercent, goal.FatEnergyPercentage);
        }

        public double GetKcalGoalPercentage()
        {
            return GetReachedPercentage(totalNutrition.Kcal, goal.Kcal);
        }

        public double GetFiberGoalPercentage()
        {
            return GetReachedPercentage(totalNutrition.Fiber, goal.FiberWeight);
        }

        
        // Helpers

        private double GetTotalProteinEnergyInKcal()
        {
            return totalNutrition.Protein * NutritionData.KcalPerGramProtein;
        }

        private double GetTotalFatEnergyInKcal()
        {
            return totalNutrition.Fat * NutritionData.KcalPerGramFat;
        }

        private double GetTotalCarbohydrateEnergyInKcal()
        {
            return totalNutrition.Carbohydrates * NutritionData.KcalPerGramCarbohydrates;
        }
    }
}
